package com.springcloud.eshop.product.service.impl;

import com.springcloud.eshop.entity.ProductProperty;
import com.springcloud.eshop.enums.DataTypeEnum;
import com.springcloud.eshop.enums.EventTypeEnum;
import com.springcloud.eshop.message.DataChangeMessage;
import com.springcloud.eshop.product.mapper.ProductPropertyMapper;
import com.springcloud.eshop.product.service.ProductPropertyService;
import com.springcloud.eshop.product.service.SendMQService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (ProductProperty)表服务实现类
 *
 * @author makejava
 * @since 2020-06-29 05:49:47
 */
@Service("productPropertyService")
public class ProductPropertyServiceImpl extends SendMQService implements ProductPropertyService {
    @Resource
    private ProductPropertyMapper productPropertyMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ProductProperty queryById(Long id) {
        return this.productPropertyMapper.queryById(id);
    }

    @Override
    public ProductProperty queryByProductId(long productId) {
        return this.productPropertyMapper.queryByProductId(productId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<ProductProperty> queryAllByLimit(int offset, int limit) {
        return this.productPropertyMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param productProperty 实例对象
     * @return 实例对象
     */
    @Override
    public ProductProperty insert(ProductProperty productProperty) {
        this.productPropertyMapper.insert(productProperty);
        sendMessage(new DataChangeMessage(EventTypeEnum.ADD.getType(), DataTypeEnum.PRODUCT_PROPERTY.getType(), productProperty.getId(), productProperty.getProductId()));
        return productProperty;
    }

    /**
     * 修改数据
     *
     * @param productProperty 实例对象
     * @return 实例对象
     */
    @Override
    public ProductProperty update(ProductProperty productProperty) {
        this.productPropertyMapper.update(productProperty);
        sendMessage(new DataChangeMessage(EventTypeEnum.UPDATE.getType(), DataTypeEnum.PRODUCT_PROPERTY.getType(), productProperty.getId(), productProperty.getProductId()));
        return this.queryById(productProperty.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        int result = this.productPropertyMapper.deleteById(id);
        sendMessage(new DataChangeMessage(EventTypeEnum.DELETE.getType(), DataTypeEnum.PRODUCT_PROPERTY.getType(), id));
        return result > 0;
    }
}